import { Node, _decorator, Component, Sprite, Event, tween, UITransform, UIOpacity, v3 } from 'cc';
// import RowFallClear_GameManager from "../Manager/GameManager";
import SpriteManager from "../Manager/SpriteManager";
import RowFallClear_GameManager from '../Manager/RowFallClear_GameManager';
import { RowFallClear_GameData } from '../Data/RowFallClear_GameData';
import CommonFunction from 'db://assets/Script/Utils/CommonFunction';

const { ccclass, property } = _decorator;

@ccclass('RowFallClear_Cell')
export default class RowFallClear_Cell extends Component {
    @property(Sprite)
    Image_item: Sprite | null = null;
    @property
    x: number = 0;
    @property
    y: number = 0;
    @property
    id: number = 1;
    @property
    target_y: number = 0;
    @property
    number: number = 0;

    // @property
    // cellSize: number = 0;

    //    // @property
    //    // isTop:boolean = false;
    @property
    isNew: boolean = false;

    onLoad() {
        this.node.on(Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        // this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        // this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }
    onMouseDown(event) {
        // let mouseType = event.getButton();
        // if (mouseType === Event.EventMouse.BUTTON_LEFT) {
        //     //            // 鼠标左键按下
        //     //            // let mousePoint = event.getLocation();
        //     //            // let localPoint = this.node.convertToNodeSpace(mousePoint);

        //     //            // TipPanel.Instance.Show("鼠标左键按下:" + String(GameManager.Instance.keyCode));

        //     // cc.log(this);
        //     return;
        // }
    }

    public Init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;
        // this.cellSize = RowFallClear_GameManager.Instance.cellSize;
        this.node.name = (this.x + 1) + "_" + (this.y + 1);
        this.node.getComponent(UITransform).width = RowFallClear_GameData.size;
        this.node.getComponent(UITransform).height = RowFallClear_GameData.size;
        // this.node.parent = RowFallClear_GameManager.Instance.Node_2D;

        RowFallClear_GameManager.Instance.Root_card.addChild(this.node);

        this.Refresh();
    }
    public Refresh() {
        this.initPosition();
        // this.RefreshPos();
        // this.RefreshUI();
    }
    public RefreshUI() {
        this.Image_item.node.active = this.id > 0;
        let spriteFrame = SpriteManager.Instance.spriteFrameLists.spriteFrameList[this.id - 1];
        this.Image_item.spriteFrame = spriteFrame;
        //        // 元素图片适配
        // let padding = 16;
        // let os = spriteFrame.getOriginalSize();
        // if (os.width > os.height) {
        //     this.Image_item.node.getComponent(UITransform).width = this.node.getComponent(UITransform).width - padding;
        //     this.Image_item.node.getComponent(UITransform).height = this.Image_item.node.getComponent(UITransform).width * (os.height / os.width);
        // }
        // else {
        //     this.Image_item.node.getComponent(UITransform).height = this.node.getComponent(UITransform).width - padding;
        //     this.Image_item.node.getComponent(UITransform).width = this.Image_item.node.getComponent(UITransform).height * (os.width / os.height);
        // }

        this.Image_item.sizeMode = Sprite.SizeMode.TRIMMED;
        CommonFunction.SetFrame(this.Image_item, RowFallClear_GameData.size - 16, RowFallClear_GameData.size - 16);
    }

    //    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    initPosition() {
        let x = -RowFallClear_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * this.x;
        let y = -RowFallClear_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * this.y;
        this.node.position = v3(x, y, 0);
    }
    // public RefreshPos() {
    //     var pos_x = RowFallClear_GameData.size * this.x - RowFallClear_GameManager.Instance.width / 2 + RowFallClear_GameData.size / 2;
    //     var pos_y = RowFallClear_GameData.size * this.y - RowFallClear_GameManager.Instance.height_screen / 2 + RowFallClear_GameData.size / 2 + RowFallClear_GameManager.Instance.padding_bottom;
    //     this.node.setPosition(pos_x, pos_y);
    // }
    public Set(_x: number, _y: number) {
        RowFallClear_GameManager.Instance.cellList[this.x][this.y] = null;
        this.x = _x;
        this.y = _y;
        RowFallClear_GameManager.Instance.cellList[this.x][this.y] = this;
    }
    public Dead() {
        RowFallClear_GameManager.Instance.cellList[this.x][this.y] = null;

        let oc = this.node.getComponent(UIOpacity);
        tween(oc)
            .to(0.3, { opacity: 80 })
            .call(() => {
                this.node.destroy();
            })
            .start()
    }
}